<template>
    <div class="shopCar-container">

        <!-- 商品列表 -->
        <div class="goods-list">

            <div class="mui-card" v-for="(item, i) in goodsList" :key="item.id">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <!-- 根据getters中的getGoodsSelected返回过来的对象，通过id键名获取selected -->
                        <mt-switch
                                v-model="$store.getters.getGoodsSelected[item.id]"
                                @change="selectedChange(item.id, $store.getters.getGoodsSelected[item.id])"></mt-switch>
                        <img :src="item.thumb_path" alt="">
                        <div class="info">
                            <h4>{{ item.title }}</h4>
                            <p>
                                <span class="price">￥{{ item.sell_price }}</span>
                                <!-- getGoodsCount中 商品id 为键名  数量为键值  -->
                                <num-box
                                        :goodsid="item.id"
                                        :count="$store.getters.getGoodsCount[item.id]"></num-box>
                                <a href="" @click.prevent="removeGoods(item.id, i)">删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        <!-- 结算区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan">
                    <div class="left">
                        <p>总计（不含运费）</p>
                        <p>已勾选商品 <span class="red">{{ sumJian }}</span> 件，
                            总价 <span class="red">￥{{ sumPrice }}</span>
                        </p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
            </div>
        </div>

    </div>
</template>

<script>
    import numBox from './childComps/ShopCar_numbox.vue'

    export default {
        name: "",
        data(){
            return {
                goodsList: []
            }
        },
        created(){
            this.getShopCarInfoList()
        },
        methods: {
            getShopCarInfoList(){
                // 从store中获取所有商品的id，然后发送请求根据id获取数据
                var idArr = []
                this.$store.state.car.forEach(item => idArr.push(item.id))

                if(idArr.length<=0) return

                this.$http.get('static/data/shopInfoList.json').then(res => {
                    if(res.body.status === 0){
                        var temp = []
                        res.body.message.forEach(item => {
                            for(var i=0; i<idArr.length; i++){
                                if(item.id === idArr[i]){
                                    temp.push(item)
                                }
                            }
                        })

                        temp.forEach(item => this.goodsList.push(item.goods))
                    }
                })
            },
            removeGoods(id, index){
                // 从store中根据id删除
                this.$store.commit('removeGoodsById', id)
                // 在当前列表中根据索引删除
                this.goodsList.splice(index, 1)
            },
            selectedChange(id, val){
                console.log(id);
                console.log(val);
                this.$store.commit('updateGoodsSelected', {
                    id: id,
                    selected: val
                })
            }
        },
        components: {
            numBox
        },
        computed: {
            sumJian(){
                var n = 0
                this.$store.state.car.forEach(item => {
                    if(item.selected){
                        n += item.count
                    }
                })
                return n
            },
            sumPrice(){
                var s = 0
                this.$store.state.car.forEach(item => {
                    if(item.selected){
                        s += (item.count * item.price)
                    }
                })
                return s
            }
        }
    }
</script>

<style lang="less" scoped>
.shopCar-container{
    .goods-list{
        background-color: #eee;
        overflow: hidden;
        .mui-card-content-inner{
            align-items: center;
            display: flex;
            img{
                width: 60px;
                height: 60px;
            }
            .info{
                h4{
                    font-size: 14px;
                    margin: 0 0 10px;
                }
                p{
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin: 0;
                    .price{
                        color: red;
                        font-weight: bold;
                    }
                }

            }
        }
    }
    .jiesuan{
        display: flex;
        justify-content: space-between;
        align-items: center;
        .red{
            font-weight: bold;
            color: red;
            font-size: 16px;
        }
    }
}
</style>